from quant_researcher.quant.project_tool.time_tool import str_to_timestamp, timestamp_to_str
import json
import pandas as pd
import requests





def fetch_okex_usdtusd_premium(frequnecy='1d'):
    """
    从https://www.okx.com/markets/data-list 获取USDT场外兑USD折溢价数据

    :return:
    """
    header = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36'}
    # date = get_today(marker='with_n_dash')
    timestamp_ = str_to_timestamp('2010-01-01') * 1000
    frequnecy_dict = {'5m': 0, '1h': 1, '1d': 2}
    url = f'https://www.okx.com/v3/c2c/tickers/premiumRate/USDT?t={timestamp_}&interval={frequnecy_dict[frequnecy]}'
    # date = timestamp_to_str(1663905947151/1000, fmt='%Y-%m-%d %H:%M:%S', tz_str='+0800')
    res = requests.get(url, headers=header, timeout=15)
    df = pd.DataFrame(json.loads(res.content)['data'])
    df['timestamps'] = df['timestamps'] / 1000
    df['datetime'] = df['timestamps'].apply(timestamp_to_str, fmt='%Y-%m-%d %H:%M:%S', tz_str='+0000')
    df['date'] = df['timestamps'].apply(timestamp_to_str, fmt='%Y-%m-%d', tz_str='+0000')
    df.rename(columns={'ratios': 'premium'}, inplace=True)

    return df


if __name__ == '__main__':
    fetch_okex_usdtusd_premium('1d')